﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFDevelopers.Samples.CommandInfo;
using WPFDevelopers.Samples.Helpers;
using WPFDevelopers.Samples.PageViews;

namespace WPFDevelopers.Samples.ViewModels
{
    public class TablePageModelVM : ViewModelBase
    {
        public List<MemberManageModelVM> ListData = new List<MemberManageModelVM>();
        public ObservableCollection<MemberManageModelVM> memberManageModels = new ObservableCollection<MemberManageModelVM>();
        public ObservableCollection<CheckBox> headerChecks = new ObservableCollection<CheckBox>();
        private MemberManageModelVM _querData = new MemberManageModelVM()
        {
            Name = "",
            Phone = "",
            CardNo = "",
            MemberType = "",
            Money = 0
        };
        public MemberManageModelVM QuerData
        {
            get { return _querData; }
            set { _querData = value; this.NotifyPropertyChange("QuerData"); CurrentPageChanged(); }
        }

        public TablePageModelVM()
        {
            for (int i = 0; i < 100; i++)
            {
                ListData.Add(new MemberManageModelVM()
                {
                    Name = "姓名" + i.ToString(),
                    CardNo = "200021" + i.ToString(),
                    Phone = "1760053" + i.ToString(),
                    Birthday = DateTime.Now.AddYears(-30).ToString("yyyy-MM-dd"),
                    Id = i.ToString(),
                    MemberType = "铂金会员",
                    Money = 105.2,
                    CreateTime = DateTime.Now.ToString("yyyy-MM-dd")
                });
            }
            Count = 100;
            CurrentPageChanged();
        }
        private int _count;
        public int Count
        {
            get { return _count; }
            set { _count = value; this.NotifyPropertyChange("Count"); CurrentPageChanged(); }
        }

        private int _countPerPage = 14;
        public int CountPerPage
        {
            get { return _countPerPage; }
            set { _countPerPage = value; this.NotifyPropertyChange("CountPerPage"); CurrentPageChanged(); }
        }

        private int _current = 1;
        public int Current
        {
            get { return _current; }
            set { _current = value; this.NotifyPropertyChange("Current"); CurrentPageChanged(); }
        }

        private void CurrentPageChanged()
        {
            headerChecks.Clear();
            memberManageModels.Clear();

            foreach (var i in ListData.Skip((Current - 1) * CountPerPage).Take(CountPerPage))
            {
                memberManageModels.Add(i);
            }
        }

        private BaseCommand clickCommand;

        public ICommand ClickCommand
        {
            get
            {
                if (clickCommand == null)
                {
                    clickCommand = new BaseCommand(Click);
                }

                return clickCommand;
            }
        }

        private void Click(object commandParameter)
        {
            var dafa = this.QuerData;
            SearchData();
        }

        private void SearchData()
        {
            ListData = ListData.Where(a => a.Name.Contains(this.QuerData.Name) && a.Phone.Contains(this.QuerData.Phone)).ToList();
            _current = 1;
            Count = ListData.Count;
            CurrentPageChanged();
        }

        private Helpers.RelayCommand delCommandAll;

        public ICommand DelCommandAll
        {
            get
            {
                if (delCommandAll == null)
                {
                    delCommandAll = new Helpers.RelayCommand(Del);
                }

                return delCommandAll;
            }
        }

        private void Del(object commandParameter)
        {
            if (commandParameter == null)
            {
                //批量删除
                var res = MessageBoxX.Question("确定要批量删除选中的会员么？");
                if (res)
                {
                    MessageBoxX.Info("删除成功");
                }
                return;
            }
            else
            {
                var model = (MemberManageModelVM)commandParameter;
                var res = MessageBoxX.Question("确定要删除该会员【" + model.Name + "】么？");
                if (res)
                {
                    MessageBoxX.Info("删除成功");
                }
                return;
            }
        }

        //RechargeCommand

        private Helpers.RelayCommand rechargeCommand;

        public ICommand RechargeCommand
        {
            get
            {
                if (rechargeCommand == null)
                {
                    rechargeCommand = new Helpers.RelayCommand(Recharge);
                }

                return rechargeCommand;
            }
        }

        private void Recharge(object obj)
        {
            var model = (MemberManageModelVM)obj;
            RechargeInfo addEditMember = new RechargeInfo(model);
            addEditMember.ShowDialog();
        }

        private Helpers.RelayCommand addOrEditMemberCommand;

        public ICommand AddOrEditMemberCommand
        {
            get
            {
                if (addOrEditMemberCommand == null)
                {
                    addOrEditMemberCommand = new Helpers.RelayCommand(AddOrEditMember);
                }

                return addOrEditMemberCommand;
            }
        }

        private void AddOrEditMember(object commandParameter)
        {


            if (commandParameter == null)
            {
                MemberInfo addEditMember = new MemberInfo(null);
                addEditMember.Title = "添加会员";
                addEditMember.ShowDialog();
            }
            else
            {

                var model = (MemberManageModelVM)commandParameter;
                MemberInfo addEditMember = new MemberInfo(model);
                addEditMember.Title = "编辑会员【  " + model.Name + "  】信息";
                addEditMember.ShowDialog();
            }
        }
    }

}
